package com.wfu.module_base.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.trello.rxlifecycle2.components.support.RxFragment;

public abstract class BaseFragment extends RxFragment {
    protected Context mContext;
    protected View rootView;
    protected LayoutInflater mInflater;
    private SparseArray<View> mViews;

    /**
     * 依附至Activity
     * <p>
     * ,hide->show的时候detach再attach的时候会再次被调用
     *
     * @param activity
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = activity;
        this.mInflater = activity.getLayoutInflater();
    }

    /**
     * 初次创建的时候都会被执行,hide->show的时候这些方法将不会再次调用
     * <p>
     * 屏幕方向改变是会销毁View
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(bindLayout(), container, false);
            mViews = new SparseArray<>();
        } else {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            parent.removeAllViews();
        }
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initResource();
        initWidget();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        this.mViews.clear();
        this.rootView = null;
        this.mContext = null;
        this.mInflater = null;

    }

    /**
     * 通过viewId 获取View，并缓存起来
     *
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(@IdRes int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = rootView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public <T extends View> T inflate(@LayoutRes int layoutId) {
        View view = mViews.get(layoutId);
        if (view == null) {
            view = mInflater.inflate(layoutId, null, false);
        }
        return (T) view;
    }

    public <T extends View> T inflate(@LayoutRes int layoutId, ViewGroup root, boolean attachToRoot) {
        View view = mViews.get(layoutId);
        if (view == null) {
            view = mInflater.inflate(layoutId, root, attachToRoot);
        }
        return (T) view;
    }

    protected void initData() {

    }

    @LayoutRes
    protected abstract int bindLayout();

    protected abstract void initResource();

    protected abstract void initWidget();
}
